<template>
  <div class="app-container">
    <!--行内的表单  进行条件查询-->
    <el-form :inline="true" ref="tbUserVO" :model="tbUserVO" class="demo-form-inline">
      <el-row>
        <el-col :span="6">
          <el-form-item label="部门" prop="departmentId">
            <el-select style="width: 220px;" v-model="tbUserVO.departmentId" placeholder="请选择">
              <el-option
                v-for="item in deptList"
                :key="item.id"
                :label="item.name"
                :value="item.id">
                <span style="float: left">{{ item.name }}</span>
                <span style="float: right; color: rgb(132, 146, 166); font-size: 13px;">
                  <span class="el-tag el-tag--success el-tag--mini el-tag--plain">{{ item.deptCount }}人</span>
                </span>
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="用户名" prop="username">
            <el-input style="width: 220px;" v-model="tbUserVO.username" placeholder="用户名"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="邮箱" prop="email">
            <el-input style="width: 220px;" v-model="tbUserVO.email" placeholder="邮箱"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="性别" prop="sex">
            <el-radio v-model="tbUserVO.sex" label="0">男</el-radio>
            <el-radio v-model="tbUserVO.sex" label="1">女</el-radio>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="6">
          <el-form-item label="昵称" prop="nickname">
            <el-input style="width: 220px;" v-model="tbUserVO.nickname" placeholder="昵称"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="18">
          <el-form-item>
            <el-button icon="el-icon-refresh" @click="resetForm('tbUserVO')">重置</el-button>
            <el-button type="primary" icon="el-icon-search" @click="userLists">查询</el-button>
            <el-button type="success" icon="el-icon-plus" @click="dialogVisible=true">添加</el-button>
            <el-button icon="el-icon-download" @click="exportUser">导出</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <!--表格区域-->
    <el-table
      :data="userList"
      border
      style="width: 100%">
      <el-table-column
        prop="id"
        label="#"
        width="50">
      </el-table-column>
      <el-table-column
        prop="username"
        label="用户名"
        width="110">
      </el-table-column>
      <el-table-column
        prop="sex"
        label="性别"
        width="100">
        <template slot-scope="scope">
          {{scope.row.sex==0?'男':(scope.row.sex==1?'女':'保密')}}
        </template>
      </el-table-column>
      <el-table-column
        prop="name"
        sortable
        label="所属部门"
        width="180">
      </el-table-column>
      <el-table-column
        prop="birth"
        sortable
        label="生日"
        width="180">
      </el-table-column>
      <el-table-column
        prop="email"
        label="邮箱"
        width="180">
      </el-table-column>
      <el-table-column
        prop="phoneNumber"
        label="电话"
        width="150">
      </el-table-column>
      <el-table-column
        prop="status"
        label="是否禁用"
        width="100">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.status==1"></el-switch>
        </template>
      </el-table-column>
      <el-table-column
        label="操作">
        <el-button type="primary" size="mini" icon="el-icon-edit"></el-button>
        <el-button type="danger" size="mini" icon="el-icon-delete"></el-button>
        <el-button type="warning" size="mini" icon="el-icon-s-tools"></el-button>
      </el-table-column>
    </el-table>
    <!--分页区域-->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="current"
      :page-sizes="[6, 10, 20, 30]"
      :page-size="size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    <!--添加用户信息的弹框-->
    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose">

      <!--用户头像上传-->
      <el-form>
        <!-- 用户头像 -->
        <el-form-item label="用户头像">

          <!-- 头衔缩略图 -->
          <pan-thumb :image="image"/>
          <!-- 文件上传按钮 -->
          <el-button type="primary" icon="el-icon-upload" @click="imagecropperShow=true">更换头像
          </el-button>

          <!--
            v-show：是否显示上传组件
            :key：类似于id，如果一个页面多个图片上传控件，可以做区分
            :url：后台上传的url地址
            @close：关闭上传组件
            @crop-upload-success：上传成功后的回调 -->
          <image-cropper
            v-show="imagecropperShow"
            :width="300"
            :height="300"
            :key="imagecropperKey"
            :url="'/ossservice/upload/uploadImgFile'"
            field="file"
            @close="close"
            @crop-upload-success="cropSuccess"/>

        </el-form-item>
      </el-form>

        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
        </span>
    </el-dialog>
  </div>
</template>

<script>
  import {getUserList,exportUserList} from '@/api/user'
  import {getDeptList} from '@/api/dept'
  import ImageCropper from '@/components/ImageCropper'
  import PanThumb from '@/components/PanThumb'

  export default {
    name: "UserList",
    components: { ImageCropper, PanThumb },
    data() {
      return {
        //用户集合
        userList: [],
        //部门集合
        deptList: [],
        //部门名称
        deptName: '',
        current: 1,
        size: 6,
        total: 100,
        tbUserVO: {
          departmentId: '',
          username: '',
          email: '',
          sex: '',
          nickname: ''
        },
        dialogVisible: false,  //是否显示添加的弹框
        imagecropperShow: false, // 是否显示上传组件
        imagecropperKey: 0, // 上传组件id
        image: 'https://wpimg.wallstcn.com/577965b9-bb9e-4e02-9f0c-095b41417191'
      }
    },
    created() {
      this.userLists();
      this.deptLists();
    },
    methods: {
      userLists() {
        getUserList(this.current, this.size, this.tbUserVO).then(res => {
          this.total = res.data.total;
          this.userList = res.data.records
        }).catch(err => {

        })
      },
      //窗体加载的时候也需要查询部门信息
      deptLists() {
        getDeptList().then(res => {
          this.deptList = res.data.deptAndCount
        }).catch(err => {

        })
      },
      exportUser() {
        exportUserList(this.tbUserVO).then(res => {
          //这里没有返回数据,但是要在响应头中进行下载操作
          // 将二进制文件转化为可访问的url
          let url = window.URL.createObjectURL(new Blob([res]));
          var a = document.createElement("a");
          document.body.appendChild(a);
          a.href = url;
          a.download = "用户列表.xls";
          a.click();
          window.URL.revokeObjectURL(url);
        }).catch(err => {

        })
      },
      //重置表单
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
      handleSizeChange(val) {
        this.size = val;
        this.userLists();
      },
      handleCurrentChange(val) {
        this.current = val;
        this.userLists();
      },
      //是否关闭添加的弹框
      handleClose(done) {
        this.$confirm('确认关闭？')
          .then(_ => {
            done();
          })
          .catch(_ => {});
      },
      // 上传成功后的回调函数
      cropSuccess(data) {
        console.log(data)
        this.imagecropperShow = false
        this.image = data.url;
        // 上传成功后，重新打开上传组件时初始化组件，否则显示上一次的上传结果
        this.imagecropperKey = this.imagecropperKey + 1
      },

      // 关闭上传组件
      close() {
        this.imagecropperShow = false
        // 上传失败后，重新打开上传组件时初始化组件，否则显示上一次的上传结果
        this.imagecropperKey = this.imagecropperKey + 1
      }
    }
  }
</script>

<style scoped>
  .avatar{
    width: 200px;
    height: 200px;
    border-radius: 50%;
  }
</style>
